package cn.net.yato.picture.util.email;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * @ClassName EmailTools
 * @Description 实现QQ邮箱验证登录,JavaMail 版本: 1.6.0 JDK 版本: JDK 1.7 以上（必须）
 * @Author yato
 * @Date 2019/10/25 9:40
 * @Version 1.0
 *
 */
@Component
public class EmailUtil {

    @Value("${mail.myemail.email}")
    private String email;
    @Resource
    private JavaMailSender mailSender;

    public boolean sendMail(String code,String senced,String emailTo){
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            helper.setFrom(new InternetAddress( "Picture Admin<"+email+">"));
            helper.setSentDate(new Date());
            helper.setTo(emailTo);
            helper.setSubject("Picture微信小程序后台登录验证码");
            helper.setText("<h2 style='color:#151835b8;text-align: center;'>这是您的登录凭证,"+senced+"分钟内有效,如非本人操作,请忽略！</h2><p style='color:#151835b8;text-align: center;'>"+code+"</p>",true);
            mailSender.send(mimeMessage);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
}
